﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Proces;

namespace MyMES.Writer.API.Application.CommendHandlers.Proces
{
    public class CreateProcessCommendHandlers : IRequestHandler<CreateProcessCommends, APIResult<Process>>
    {

        private readonly IBaseRepository<Process> processRepository;
        private readonly IMapper mapper;

        public CreateProcessCommendHandlers(IBaseRepository<Process> processRepository, IMapper mapper)
        {
            this.processRepository = processRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 创建工艺流程
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Process>> Handle(CreateProcessCommends request, CancellationToken cancellationToken)
        {

            APIResult<Process> result = new APIResult<Process>();
            result.Code = APIEnum.添加成功;
            result.Msg= "添加成功";
            var process = mapper.Map<Process>(request);
            string sql = "insert into Process(ProcessID,ProcessNo,ProcessName,Processroute,Processdescris,isvailable,IsDel) values(@ProcessID,@ProcessNo,@ProcessName,@Processroute,@Processdescris,@isvailable,@IsDel)";
            await processRepository.Insert(process,sql);
            return  result;
        }
    }
}
